﻿using RStudio.InputManager;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;


namespace BackpackSystem
{
    /// <summary>
    /// 
    /// </summary>
    public class Item : MonoBehaviour
    {
        private void Start()
        {
            InputCenter.Get<MouseInteraction>(this.gameObject).OnDragBegin += OnItemDragBegin;
            InputCenter.Get<MouseInteraction>(this.gameObject).OnDragging += OnItemDraging;
            InputCenter.Get<MouseInteraction>(this.gameObject).OnDragEnd += OnItemDragEnd;
        }


        [Tooltip("数据|资产信息")] public ItemBase m_ItemBase;
        [Tooltip("数据|旧的槽位")] public ItemSlot m_OldItemSlot;
        [Tooltip("图标|物品信息"), SerializeField] Image m_IMGIcon;
        [Tooltip("文本|物品详细"), SerializeField] TextMeshProUGUI m_TMPDescription;

        [Header("")]
        [Tooltip("组件|画布位置"), SerializeField] RectTransform m_RectTransform;
        [Tooltip("组件|关联库存"), SerializeField] InventoryUI m_InventoryUI;


        Vector3 m_Origin;

        public void DoBind(InventoryUI inventoryUI, ItemBase itemBase, ItemSlot itemSlot)
        { 
            // 更新引用
            this.m_InventoryUI = inventoryUI;
            this.m_ItemBase = itemBase;
            this.m_OldItemSlot = itemSlot;

            // 更新表现
            this.m_IMGIcon.sprite = itemBase.Icon;
            this.m_TMPDescription.text = itemBase.Description;

            // 订阅事件
            InputCenter.Get<MouseInteraction>(this.gameObject).OnEnter -= OnItemInfo;
            InputCenter.Get<MouseInteraction>(this.gameObject).OnEnter += OnItemInfo;
        }
        public void DoUpdate()
        {
            transform.GetComponent<RectTransform>().position = m_OldItemSlot.GetComponent<RectTransform>().position;
        }

        

        private void OnItemInfo(PointerEventData data)
        {
            this.m_InventoryUI.Submit(this.m_ItemBase);
        }


        private void OnItemDragBegin(PointerEventData data)
        {
            this.transform.GetComponent<Canvas>().sortingOrder = 51;
            this.m_Origin = this.transform.GetComponent<RectTransform>().position;
        }
        private void OnItemDraging(PointerEventData data)
        {
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                this.transform.parent.GetComponent<RectTransform>(),
                Mouse.current.position.value,
                this.m_InventoryUI.CanvasRoot.worldCamera,
                out Vector2 output);

            this.m_RectTransform.localPosition = output;
        }
        private void OnItemDragEnd(PointerEventData data)
        {
            this.transform.GetComponent<Canvas>().sortingOrder = 50;

            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                this.m_InventoryUI.InventoryRootRT,
                Mouse.current.position.value,
                this.m_InventoryUI.CanvasRoot.worldCamera,
                out Vector2 point);


            bool isGet = this.m_InventoryUI.TryGetItemSlot(point, out ItemSlot output);
            if (!isGet)
            {
                this.transform.GetComponent<RectTransform>().position = m_Origin;
                return;
            }

            if (output.IsPlaced)
            {
                this.transform.GetComponent<RectTransform>().position = m_Origin;
                return;
            }


            this.m_OldItemSlot.DoUpdateDetail(null);
            this.m_OldItemSlot = output;
            this.m_OldItemSlot.DoUpdateDetail(this);

            transform.position = output.GetComponent<RectTransform>().position;

        }
    }
}
